package com.dayuanit.service.impl;

import com.dayuanit.controller.request.AddWarehouseRecordParam;
import com.dayuanit.dao.EquipmentDao;
import com.dayuanit.dao.WarehouseRecordDao;
import com.dayuanit.entity.Equipment;
import com.dayuanit.entity.WarehouseRecord;
import com.dayuanit.service.WarehouseRecordService;
import com.dayuanit.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WarehouseRecordServiceImpl implements WarehouseRecordService {


    @Autowired(required = false)
    WarehouseRecordDao warehouseRecordDao;

    @Autowired(required = false)
    EquipmentDao equipmentDao;

    @Override
    public PageInfo queryWarehouseRecordList(Integer page, Integer pageSize) {
        Integer count = warehouseRecordDao.countBy();
        PageInfo pageInfo = new PageInfo().setPageNo(page).setPageSize(pageSize).setTotalCount(count);
        if(count == 0L) {
            return pageInfo.setData(new ArrayList());
        }
        List<Equipment> equipmentList = warehouseRecordDao.queryWarehousePage(pageInfo.limitStart(),pageInfo.limitEnd());
        return pageInfo.setData(equipmentList);
    }

    @Override
    public void addWarehouseRecord(AddWarehouseRecordParam param) {
        Equipment equipment = equipmentDao.findById(param.getEquipmentId());
        WarehouseRecord warehouseRecord = new WarehouseRecord();
        warehouseRecord.setEquipmentId(param.getEquipmentId());
        warehouseRecord.setEquipmentName(equipment.getEquipmentName());
        warehouseRecord.setInAndOutType(param.getInAndOutType());
        warehouseRecord.setCheckUserName(param.getCheckUserName());
        warehouseRecord.setLastUpdateTime(new Date());
        warehouseRecord.setAddTime(new Date());
        warehouseRecord.setIsDelete(0);
        warehouseRecordDao.insert(warehouseRecord);
    }


    @Override
    public void batchDeleteWarehouseRecord(List<Long> idList) {
        if(idList.size() > 0) {
            warehouseRecordDao.batchRemove(idList);
        }
    }
}
